import os
import sys
from helper import get_parser, load_config
from channels.register import channels_modules
from utils.lines_prcd import save_lines
from utils.params import args_show
from utils.args_quick_call import Arguments

def main():
    # load default_args
    args = get_parser()
    args_dict = args.__dict__
    
    # load config.yaml
    config_file = args.config
    config_file = os.path.join(args.base, 'config', config_file)
    loder = load_config(config_file)
        
    args_dict.update(loder)
    args_show(args_dict)
    args_dict = Arguments(**args_dict)
    print('args_dict: ', args_dict)
    print(args_dict['file_path'])
    print('*' * 100)

    # global variable   
    context = {
        'channels': {
            'extractions': [],
            'formatters': []
        },
        'content': '',
        'lines': [],
        'args': args_dict,
        'meta': {}
    }

    # Channels
    print('***********Channels Loading!***********')
    # -- load_channels
    register_modules = channels_modules()

    # -- extractions
    extractions_channels = args_dict.get('extractions', [])
    formatters_channels = args_dict.get('formatters', [])
    context['channels'].update({
        'extractions': extractions_channels,
        'formatters': formatters_channels
    })    


    register_extractions = register_modules.get('extractions', {})
    for extract in extractions_channels:
        cur_extract_clazz = register_extractions.get(extract)
        
        # channels check path
        cur_extract_clazz.check(args_dict)

        print(str(cur_extract_clazz), 'Start!')
        up_datas = cur_extract_clazz.execute(**context)        
        print(str(cur_extract_clazz), 'End!')
        context.update(up_datas)
        
    # -- formatters
    register_formatters = register_modules.get('formatters', {})
    for formatter in formatters_channels:
        cur_formatter_clazz = register_formatters.get(formatter)

        # channels check path
        cur_formatter_clazz.check(args_dict)

        print(str(cur_formatter_clazz), 'Start!')
        up_datas = cur_formatter_clazz.execute(**context)
        print(str(cur_formatter_clazz), 'End!')
        context.update(up_datas)
    
    print('*************Channels End!*************')

    # save lines
    save_lines(args_dict, context['lines'])
    


if __name__ == '__main__':
    main()